﻿using System;
using System.Linq;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.Globalization;

namespace Medianamik.UI.Web.ModalWindows.ContentManagement
{
    public partial class CultureSelectorWindow : BasePage
    {
        Guid _nodeID;

        protected void Page_Load(object sender, EventArgs e)
        {
            var strNodeId = Request.QueryString[DefaultParamNames.NodeID];

            if (StringHelper.IsGuid(Request.QueryString[DefaultParamNames.NodeID], out _nodeID))
            {
                var node = NodeManager.GetNode(_nodeID);
                if (node == null)
                {
                    ShowError(String.Concat(
                        (string)GetGlobalResourceObject("Medianamik_Global", "NodeNotFound"),
                        " ", strNodeId));
                }
                else
                {
                    ddlCultures.DataSource = node.Instances.Where(i =>
                        i.Culture != CurrentEditionCulture && i.Culture.Name != "")
                        .Select(i => i.Culture);
                    ddlCultures.DataBind();
                    ddlCultures.Items.Insert(0, Translator.Translate("Medianamik_ModalWindows", "CultureSelector_NoDuplicateCultureOption"));
                }
            }
            else
            {
                ShowError(String.Concat(
                    (string)GetGlobalResourceObject("Medianamik_Global", "ArgumentNullException"), " ",
                    "nodeid"));
            }
        }

        protected Guid NodeId
        {
            get { return _nodeID; }
        }

        private void ShowError(string error)
        {
            phrMain.Visible = false;
            phrError.Visible = true;
            litError.Text = error;
        }
    }
}
